package com.eascs.workflow.wf.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.RedirectButton;
import com.eascs.tag.MenuTag;
import com.eascs.workflow.common.controller.BaseMasterViewController;
import com.eascs.workflow.common.entity.sys.SysRole;
import com.eascs.workflow.common.entity.wf.DicData;
import com.eascs.workflow.common.entity.wf.ProcessNodeTemplate;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.dictionary.dao.DicDataDao;
import com.eascs.workflow.sys.service.SysPermissionServiceInlet;
import com.eascs.workflow.wf.dao.ProcessNodeTemplateDao;
import com.eascs.workflow.wf.dao.ProcessTemplateDao;
import com.eascs.workflow.wf.service.WFProcessService;

@Controller
public class WorkFlowViewController extends BaseMasterViewController {
	private static final Logger logger = LoggerFactory.getLogger(WorkFlowViewController.class);
	@Autowired
	private ProcessTemplateDao processTemplateDao;
	@Autowired
	private ProcessNodeTemplateDao processNodeTemplateDao;
	@Autowired
	private DicDataDao dicDataDao;
	@Autowired
	private SysPermissionServiceInlet sysPermissionServiceInlet;
	@Value("${o2o.workflow.iframe.domain}")
	private String domain;
	@Autowired
	private WFProcessService wfService;

	@Override
	public void preHandle() {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "systemSettings");
	}

	@RequestMapping("/process/template/list")
	public String getProcessList(String processName, Integer pageIndex) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "wfTemplateManage");
		PageResult<Object[]> pageResult = new PageResult<Object[]>();
		if (pageIndex == null) {
			pageIndex = 0;
		}
		Integer pageSize = 15;
		Map<String, String> param = new HashMap<String, String>();
		param.put("processName", StringUtils.trimToEmpty(processName));
		pageResult = processTemplateDao.getPocessTemplateList(param, pageSize, pageIndex);
		String[] titles = { "名称", "种类", "类型", "描述", "系统模块", "发布状态" };
		Button[] bottons = new Button[] { new RedirectButton("查看", "/wf/template/view?processNo="),
				new AjaxButton("禁用", "/wf/template/isEnable?isEnable=N&processNo="),
				new AjaxButton("启用", "/wf/template/isEnable?isEnable=Y&processNo="),
				new AjaxButton("复制", "/process/copyTemplate?processNo="),
				new AjaxButton("删除", "/wf/process/del?processNo=") };

		SelectDataOption[] selectDataOptions = { null, new SelectDataOption("processCatagory"),
				new SelectDataOption("processType"), null, null, new SelectDataOption("PUBLISH_STATUS"), null };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, "/process/template/list?pageIndex=");
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setSelectDataOptions(selectDataOptions);
		paginationInfo.setMoreButtons(bottons);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("processName", processName);
		return "/wf/template/process-template-list";
	}

	@RequestMapping("/process/auditors")
	public String getProcessAuditors(String orgNo, String processType) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "processAuditorView");
		List<Map<String, Object>> auditors = new ArrayList<Map<String, Object>>();
		if (StringUtils.isNotBlank(orgNo)) {
			auditors = wfService.getProcessAuditors(processType, orgNo);
		}
		ViewContext.put("auditors", auditors);
		return "/wf/template/proecess-auditors-list";
	}

	/**
	 * 流程添加
	 * 
	 * @return
	 */
	@RequestMapping("/process/add/index")
	public String addProcessIndex(String processTemplateId) {
		if (StringUtils.isNoneBlank(processTemplateId)) {
			ProcessTemplate processTemplate = processTemplateDao.getById(ProcessTemplate.class, processTemplateId);
			ViewContext.put("processTemplate", processTemplate);
		}
		// List<DicData> dicData = dicDataDao.getDicDataByType("processType");
		// ViewContext.put("dicData", dicData);
		return "/wf/template/process-template-add";
	}

	/**
	 * 流程节点添加
	 * 
	 * @param nodeTemplateId
	 * @return
	 */
	@RequestMapping("/nodeTemplate/add/index")
	public String nodeTemplateAddIndex(String processTemplateId, String nodeTemplateId) {
		ModelResult<List<SysRole>> modelResult = sysPermissionServiceInlet.getRoleList();
		List<SysRole> roleList = modelResult.getModel();
		ViewContext.put("roleList", roleList);
		ViewContext.put("processTemplateId", processTemplateId);
		ViewContext.put("nodeTemplateId", nodeTemplateId);
		if (StringUtils.isNotBlank(nodeTemplateId)) {
			ProcessNodeTemplate processNodeTemplate = processNodeTemplateDao.getById(ProcessNodeTemplate.class,
					nodeTemplateId);
			ViewContext.put("processNodeTemplate", processNodeTemplate);
		}
		List<DicData> dicData = dicDataDao.getDicDataByType("processNodeType");
		ViewContext.put("dicData", dicData);
		return "/wf/template/node-template-add";
	}
}
